package com.example.demo;

import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by IntelliJ IDEA.
 * User: zm
 * Date: 2025/2/8
 */
@RestController
@RequestMapping("/request")
public class UserController {
    //获得cookie的传统方式
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();

        if(cookies!=null){
            for(Cookie c: cookies){
                System.out.println(c.getName()+":"+c.getValue());
            }
            return "获取Cookie成功";
        }
        return "Cookie 为空";
    }
    //只能获取指定的cookies
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("bite") String bite){
        return "bite: " + bite;
    
    }

}
